import axios from "./axios";

// uploadFile
export function uploadFile(url: string, data: any = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data, {
        headers: { "Content-Type": "multipart/form-data" },
      })
      .then((response: any) => {
        resolve(response);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

// get
export function get(
  url: string,
  params: any = {},
  responseType: any = "json"
): Promise<any> {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
        responseType: responseType,
      })
      .then((response: any) => {
        resolve(response);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

// post
export function post(url: string, data: any = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data)
      .then((response: any) => {
        resolve(response);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

// delete
export function del(url: string, data: any = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios
      .delete(url, { data })
      .then((response: any) => {
        resolve(response);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}

// put
export function put(url: string, data: any = {}): Promise<any> {
  return new Promise((resolve, reject) => {
    axios
      .put(url, data)
      .then((response: any) => {
        resolve(response);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
}
